package com.yxwl.demo.ui.print;


import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.print.base.bean.PrinterConstantPool;
import com.print.printer.USBPrinterManage;
import com.yxwl.demo.R;
import com.yxwl.demo.ui.print.printer.PrinterUtil;

public class PrintUSBActivity extends PrintActivity {

    private ActivityResultLauncher<String> storagePermissionLauncher;

    @Override
    protected void initView() {
        super.initView();
        ((TextView) findViewById(R.id.menu_connect)).setText("开启服务");
        ((TextView) findViewById(R.id.menu_disconnect)).setText("关闭服务");
        storagePermissionLauncher = registerForActivityResult(
                new ActivityResultContracts.RequestPermission(), result -> {
                    if (result) {
                        USBPrinterManage.getInstance().startUSBServer();
                        menuContent.setVisibility(View.GONE);
                    }
                }
        );
    }

    @Override
    protected void widgetClick(View view, int id) {
        if (id == R.id.menu_connect) {
            storagePermissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            return;
        }

        if (id == R.id.menu_disconnect) {
            USBPrinterManage.getInstance().stopUSBServer();
            menuContent.setVisibility(View.GONE);
            return;
        }
        if (id == R.id.menu_info) {
            if (PrinterUtil.getInstance().isConnect()) {
                Bundle bundle = new Bundle();
                bundle.putInt("type", PrinterConstantPool.SocketType.USB);
                startAct(PrinterInfoActivity.class, bundle);
            }
            menuContent.setVisibility(View.GONE);
            return;
        }
        super.widgetClick(view, id);
    }

    protected void setTitle() {
        TextView titleTv = findViewById(R.id.title);
        titleTv.setText("A80");
    }

    @Override
    public void initPrinter() {
        super.initPrinter();
        PrinterUtil.getInstance().initPrintUtil("A80");
    }

    protected void connect() {
    }

}